﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace BrokenDelusionMod.Manager
{
    public static class Log
    {
        private const int STD_OUTPUT_HANDLE = -11;
        public static string inputString;
        public static event Action<string> OnInputText;

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool AttachConsole(uint dwProcessId);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool AllocConsole();

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool FreeConsole();

        [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr GetStdHandle(int nStdHandle);

        [DllImport("kernel32.dll")]
        static extern bool SetConsoleTitle(string lpConsoleTitle);

        public static void iniLog()
        {

            if (!AttachConsole(0x0ffffffff))
            {
                AllocConsole();
            }
            IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
            Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(stdHandle, true);
            FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
            StreamWriter standardOutput = new StreamWriter(fileStream, Encoding.ASCII);
            Console.OutputEncoding = Encoding.ASCII;
            standardOutput.AutoFlush = true;
            Console.SetOut(standardOutput);
            OnInputText = Mod.sendMessage;
            Console.WriteLine("Log Initialize Success");

        }

        public static void modLog(string log)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(log);
        }

        public static void ClearLine()
        {
            Console.CursorLeft = 0;
            Console.Write(new String(' ', Console.BufferWidth));
            Console.CursorTop--;
            Console.CursorLeft = 0;
        }

        public static void RedrawInputLine()
        {
            if (inputString.Length == 0) return;

            if (Console.CursorLeft > 0)
                ClearLine();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(inputString);
        }
        internal static void OnBackspace()
        {
            if (inputString.Length < 1) return;

            inputString = inputString.Substring(0, inputString.Length - 1);
            RedrawInputLine();
        }

        internal static void OnEscape()
        {
            ClearLine();
            inputString = "";
        }

        internal static void OnEnter()
        {
            ClearLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("> " + inputString);

            var strtext = inputString;
            inputString = "";

            if (OnInputText != null)
            {
                OnInputText(strtext);
            }
        }

        public static void Update()
        {
            if (!Console.KeyAvailable) return;
            var key = Console.ReadKey();

            if (key.Key == ConsoleKey.Enter)
            {
                OnEnter();
                return;
            }

            if (key.Key == ConsoleKey.Backspace)
            {
                OnBackspace();
                return;
            }

            if (key.Key == ConsoleKey.Escape)
            {
                OnEscape();
                return;
            }

            if (key.KeyChar != '\u0000')
            {
                inputString += key.KeyChar;
                RedrawInputLine();
                return;
            }
        }
    }
}
